/*
 * GetAccount.java
 *
 * Created on 7 novembre 2007, 10:36
 */
package com.reimscopie.console ;

import java.io.* ;

import javax.naming.* ;
import javax.transaction.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * This command get a user account.
 */
public class GetAccount extends BeanCommand {
  
  /**
   * Create a new <code>GetAccount</code> instance.
   */
  public GetAccount() {
    
    super("com.reimscopie.beans.session.ReimsCopieAdminBean",
        ReimsCopieAdminRemote.class) ;
    
  }
  
  
  /**
   * Return the name of this command. The name is the first word in the shell
   * command line.
   *
   * @return The command name.
   */
  public String getName() {
    return "get-account" ;
  }
  
  
  /**
   * Return a short help message about this command.
   *
   * @return The command help.
   */
  public String getHelp() {
    return "Get an existing user account" ;
  }
  
  
  /**
   * Return the command usage. This string must give all command options with
   * a description for each option.
   *
   * @return The command usage.
   */
  public String getUsage() {
    
    StringBuilder builder = new StringBuilder() ;
        
    builder.append("get-account -login l | -id id\n") ;
    builder.append("  l    The user login\n") ;
    builder.append("  id   The user id\n") ;
    
    return builder.toString() ;
  }
  
  
  /**
   * Execute this command.
   *
   * @param args The arguments given in the command line.
   * @param output The command output.
   *
   * @exception CommandException Arguments are invalid.
   */
  public void execute(String[] args,ConsoleOutput output)
  throws CommandException {
    
    String login = getOption("login",args) ;
    String id    = getOption("id",args) ;
    if (login == null  &&  id == null) {
      output.printError("Login or ID required",true) ;
      return ;
    }
    
    try {
      // get JNDI initial context
      Context context = new InitialContext() ;
      UserTransaction transaction = getTransaction(context) ;
      ReimsCopieAdminRemote bean = (ReimsCopieAdminRemote)getBean(context) ;
      
      if (bean == null) {
        output.printError("account not created (no bean)",true) ;
        return ;
      }
      
      try {
        transaction.begin() ;
        
        // lookup
        Account account;
        if (login != null)
          account = bean.getAccount(login) ;
        else
          account = bean.getAccount(Long.parseLong(id)) ;
        
        // ouput
        if (account == null)
          output.print("no such account",true) ;
        else
          output.print(account.toString(),true) ;
        
        transaction.commit() ;
        output.print("Done",true) ;
      }
      catch (Exception e) {
        output.printError("Failed",true) ;
        try {
          transaction.rollback() ;
        }
        catch (SystemException ex) {
          output.printError(ex) ;
        }
        output.printError(e) ;
      }
      
    }
    catch (NamingException e) {
      output.printError("Cannot get initial context for JNDI",true) ;
      output.printError(e) ;
      return ;
    }
  }
  
}
